package cn.gaily.eduservice.service.impl;

import cn.gaily.commonutils.R;
import cn.gaily.eduservice.entity.EduCourse;
import cn.gaily.eduservice.entity.EduCourseDescription;
import cn.gaily.eduservice.entity.vo.CourseInfoVo;
import cn.gaily.eduservice.entity.vo.CoursePublishVo;
import cn.gaily.eduservice.mapper.EduCourseMapper;
import cn.gaily.eduservice.service.EduChapterService;
import cn.gaily.eduservice.service.EduCourseDescriptionService;
import cn.gaily.eduservice.service.EduCourseService;
import cn.gaily.eduservice.service.EduVideoService;
import cn.gaily.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author xuchengwei
 * @since 2021-07-05
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if(insert <= 0) {
            throw new GuliException(20001, "添加课程基本信息失败");
        }
        String id = eduCourse.getId();
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo, eduCourseDescription);
        eduCourseDescription.setId(id);
        eduCourseDescriptionService.save(eduCourseDescription);
        return id;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(eduCourseDescription.getDescription());
        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if(update == 0){
            throw new GuliException(20000,"修改课程信息失败");
        }
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescriptionService.updateById(eduCourseDescription);
    }

    @Override
    public CoursePublishVo getpublishCourseInfo(String id) {
        CoursePublishVo coursePublishVo = baseMapper.getPublicCourseInfo(id);
        return coursePublishVo;
    }

    @Override
    public int removeCourse(String courseId) {
        eduVideoService.removeVideoByCourseId(courseId);
        eduChapterService.removeById(courseId);
        eduCourseDescriptionService.removeById(courseId);
        int delete = baseMapper.deleteById(courseId);
        if(delete == 0){
            throw new GuliException(20001,"删除失败");
        }
        return delete;
    }


}
